﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSPhuCapChucVu : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string LoaiPhuCap { get; set; }
        public void EditPCCV(int hdId, decimal heSo, int loaiPhuCapId, DateTime ngayHieuLuc, DateTime ngayHetHieuLuc, string ghiChu,string loaiPhuCap)
        {
            List<NSPhuCapChucVu> lst = (List<NSPhuCapChucVu>)HttpContext.Current.Session["PCCV"];
            NSPhuCapChucVu pc = lst.Where(a => a.PhuCapId.Equals(hdId)).SingleOrDefault();
            pc.NgayHieuLuc = ngayHieuLuc;
            pc.NgayHetHieuLuc = ngayHetHieuLuc;
            pc.GhiChu = ghiChu;
            pc.LoaiPhuCapChucVuID = loaiPhuCapId;
            pc.HeSoPhuCap = heSo;
            pc.LoaiPhuCap = loaiPhuCap;
        }
        public void DeletePCCV(int hdId)
        {
            List<NSPhuCapChucVu> lst = (List<NSPhuCapChucVu>)HttpContext.Current.Session["PCCV"];
            NSPhuCapChucVu pc = lst.Where(a => a.PhuCapId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllPCCVByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSPhuCapChucVus
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count>0)
            {
                db.NSPhuCapChucVus.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }
        
        }
        public void UpdatePCCV_NhanVien(int nhanVienId, List<NSPhuCapChucVu> pccvObject)
        {
           
            var queryPCCVByNhanVienId = (from a in db.NSPhuCapChucVus
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryPCCVByNhanVienId.Count>0)
            {
                db.NSPhuCapChucVus.DeleteAllOnSubmit(queryPCCVByNhanVienId);
                db.SubmitChanges();
            }
         
            foreach (NSPhuCapChucVu pc in pccvObject)
            {
                NSPhuCapChucVu item = new NSPhuCapChucVu();
                CopyNsPhuCapchucvu(pc, item);
                db.NSPhuCapChucVus.InsertOnSubmit(item);
            }
            db.SubmitChanges();
           
        }
        private void CopyNsPhuCapchucvu(NSPhuCapChucVu src ,NSPhuCapChucVu des)
        {
            des.LoaiPhuCap = src.LoaiPhuCap;
            des.HeSoPhuCap = src.HeSoPhuCap;
            des.NgayHetHieuLuc = src.NgayHetHieuLuc;
            des.NgayHieuLuc = src.NgayHieuLuc;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.LoaiPhuCapChucVuID = src.LoaiPhuCapChucVuID;

        }
        public List<NSPhuCapChucVu> LoadPCCVByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSPhuCapChucVus
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.NgayHetHieuLuc descending
                         select a).Take(3).ToList();
            foreach (NSPhuCapChucVu item in query)
            {
                item.LoaiPhuCap = common.LoadCommonCodeById((int)item.LoaiPhuCapChucVuID);
            }
            return query;

        }
    }

}